-- upgrade_equip
-- Created by chengb Jul/15/2015
-- 成就：强化装备（升级装备）

-- 内部函数声明
local whenUpgradeEquip;

-- 注册玩家强化装备的回调
EventMgr.register("achievements/upgrade_equip", event.UPGRADE_EQUIP, function(args)
    whenUpgradeEquip(args);
end);

-- 注册玩家强化装备的回调
whenUpgradeEquip = function(args)
    local classId = args.nextClassId;

    if type(classId) ~= "number" then
        return;
    end

    -- 是否工坊升级
    local isSmith = args.isSmith;

    local baseClassId = EquipM.findBaseEquip(classId);
    local level = EquipM.query(classId, "level");

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    local layer = DungeonM.currentLayer();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("upgrade_equip");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end

        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["is_smith"]) == 'number' and
                isSmith ~= ruleArg["is_smith"] then
                -- 不是工坊升级的装备
                return false;
            end

            if type(ruleArg["class_id"]) == 'number' and
                ruleArg["class_id"] ~= baseClassId then
                -- 不是需求的装备
                return false;
            end

            if type(ruleArg["level"]) == 'number' and
               level < ruleArg["level"] then
                -- 等级不达标
                return false;
            end
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};